package com.easondongh.blog.service.impl;

import com.easondongh.blog.dao.CommentDao;
import com.easondongh.blog.dao.CommentGoodsDao;
import com.easondongh.blog.mapper.BlogMapper;
import com.easondongh.blog.mapper.UserMapper;
import com.easondongh.blog.pojo.Blog;
import com.easondongh.blog.pojo.Comment;
import com.easondongh.blog.pojo.CommentGoods;
import com.easondongh.blog.pojo.User;
import com.easondongh.blog.service.CommentService;
import com.easondongh.blog.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author EasonDongH
 * @date 2020/4/8 15:03
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentGoodsDao commentGoodsDao;

    @Override
    public void save(Comment comment) {
        // 该博客的评论数+1
        Blog blog = blogMapper.getById(comment.getCommentBlog());
        blog.setBlogComment(blog.getBlogComment() + 1);
        this.blogMapper.update(blog);
        comment.setCreatedTime(DateTimeUtils.DateToString(new Date()));
        comment.setCommentGood(0);
        // 设置文章
        blog.setBlogContent(null);
        comment.setBlog(blog);
        // 设置评论用户
        User loginUser = (User) ShiroUtils.getLoginUser();
        comment.setCommentUser(loginUser.getUserId());
        comment.setUser(loginUser);
        comment.setId(idWorker.nextId() + "");
        this.commentDao.save(comment);
    }

    @Override
    public List<Comment> getListByBlogId(String id) {
        List<Comment> commentList =  this.commentDao.findByCommentBlogOrderByCreatedTimeDescCommentGoodDesc(id);
        User loginUser = (User) ShiroUtils.getLoginUser();
        if(loginUser != null) {
            // 当前已登录，则判断评论列表中哪些已点赞
            Map<String, Comment> commentMap = commentList.stream().collect(Collectors.toMap(Comment::getId, Function.identity()));
            List<CommentGoods> goodsList = this.commentGoodsDao.findByUserIdAndBlogId(loginUser.getUserId(), id);
            goodsList.forEach(good -> {
                Comment comment = commentMap.get(good.getCommentId());
                if(comment != null) {
                    comment.setCommentFlag(true);
                }
            });
        }
        return commentList;
    }

    @Override
    public void good(CommentGoods commentGoods) {
        // 更新评论的点赞数
        Comment comment = this.commentDao.findById(commentGoods.getCommentId()).get();
        comment.setCommentGood(comment.getCommentGood() + 1);
        this.commentDao.save(comment);
        // 存入点赞记录
        commentGoods.setId(idWorker.nextId() + "");
        this.commentGoodsDao.save(commentGoods);
    }

    @Override
    public boolean hasGoodByCommentAndUser(Integer userId, String commentId) {
        return this.commentGoodsDao.countByUserIdAndCommentId(userId, commentId) > 0;
    }

    @Override
    public Page<Comment> getByPage(Page<Comment> page) {
        User user = (User) ShiroUtils.getLoginUser();
        Comment comment = new Comment();
        comment.setCommentUser(user.getUserId());
        Example<Comment> example = Example.of(comment);
        Pageable pageable = PageRequest.of(page.getCurrentPage() - 1, page.getPageSize());
        org.springframework.data.domain.Page<Comment> p = commentDao.findAll(example, pageable);
        // 封装总页数、总条数、数据
        page.setTotalCount((int)p.getTotalElements());
        page.setTotalPage(p.getTotalPages());
        page.setList(p.getContent());
        return page;
    }

    @Override
    public Page<Comment> getByPageBack(Page<Comment> page) {
        Comment comment = new Comment();
        String blogTitle = (String) page.getParams().get("blogTitle");
        if(StringUtils.isBlank(blogTitle)) {
            blogTitle = "";
        }
        String nickname = (String) page.getParams().get("nickname");
        if(StringUtils.isBlank(nickname)) {
            nickname = "";
        }
        Blog blog = new Blog();
        blog.setBlogTitle(blogTitle);
        comment.setBlog(blog);
        User user = new User();
        user.setNickname(nickname);
        comment.setUser(user);
        Pageable pageable = PageRequest.of(page.getCurrentPage() - 1, page.getPageSize());
        org.springframework.data.domain.Page<Comment> p = commentDao.getByBlogTitleAndNickname(comment, pageable);
        // 封装总页数、总条数、数据
        page.setTotalCount((int)p.getTotalElements());
        page.setTotalPage(p.getTotalPages());
        page.setList(p.getContent());
        return page;
    }

    @Override
    public boolean deleteById(String id) {
        this.commentDao.deleteById(id);
        return true;
    }
}
